package com.mdd.car.service.impl;

import com.mdd.car.params.CarModelsParam;
import com.mdd.car.service.ICarModelsService;
import com.mdd.car.vo.CarModelsVo;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdd.car.entity.CarModels;
import com.mdd.car.mapper.CarModelsMapper;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 车型
 * @Description
 * @Author 幻速
 * @Date 2024/3/11 10:13
 **/
@Service
public class CarModelsServiceImpl extends ServiceImpl<CarModelsMapper, CarModels> implements ICarModelsService {
    @Override
    public List<CarModelsVo> all() {
        LambdaQueryWrapper<CarModels> carModelsLQW=new LambdaQueryWrapper<>();
        List<CarModelsVo> carModelsVos=new ArrayList<>();
        this.baseMapper.selectList(carModelsLQW).forEach(carModels -> {
            CarModelsVo carModelsVo=new CarModelsVo();
            BeanUtil.copyProperties(carModels,carModelsVo);
            carModelsVos.add(carModelsVo);
        });
        return carModelsVos;
    }
    @Override
    public PageResult<CarModelsVo> list(PageValidate pageValidate,String name) {
        int page = null != pageValidate ? pageValidate.getPageNo() : 1;
        int limit = null != pageValidate ? pageValidate.getPageSize() : 10;
        LambdaQueryWrapper<CarModels> carBrandLQW=new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            carBrandLQW.like(CarModels::getName, name);
        }
        IPage<CarModels> iPage = this.baseMapper.selectPage(new Page<>(page, limit), carBrandLQW);
        List<CarModelsVo> list = new LinkedList<>();
        for (CarModels item : iPage.getRecords()) {
            CarModelsVo vo = new CarModelsVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }
        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    @Override
    public void add(CarModelsParam carModelsParam) {
        CarModels carModels=new CarModels();
        BeanUtils.copyProperties(carModelsParam,carModels);
        save(carModels);
    }

    @Override
    public void edit(CarModelsParam carBrandParam) {
        CarModels carModels=new CarModels();
        BeanUtils.copyProperties(carBrandParam,carModels);
        updateById(carModels);
    }

    @Override
    public void del(List<Long> ids) {
        this.baseMapper.deleteBatchIds(ids);
    }
}
